using System.Drawing;
using System.Windows.Forms;

namespace TouchSmart.Drawing
{
    public class BackBuffer
    {
        private readonly Control _control;
        
        public Bitmap Buffer { get; private set; }
        public Graphics Graphics { get; private set; }

        public BackBuffer(Control control)
        {
            this._control = control;
            this._control.Resize += (sender, e) => InitializeBuffer();

            InitializeBuffer();
        }

        private void InitializeBuffer()
        {
            Buffer = new Bitmap(this._control.Width, this._control.Height);

            if (Graphics != null)
                Graphics.Dispose();

            Graphics = Graphics.FromImage(Buffer);
        }
    }
}